package com.gdk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.service.PromotionService;
import com.gdk.service.ShopService;
import com.gdk.utils.XVDateUtils;

/**
 * 文章controller
 * */

@Controller
public class ArticleController {
	
	@Autowired
    private ArticleService articleService;
	@Autowired
    private NavigationService navigationService;	
	@Autowired
    private ShopService shopService;	
	@Autowired
    private PromotionService promotionService;	
	/**
	 * 新增文章前查询模块
	 * 
	 * */
	
	@RequestMapping("/preAddArticle.do")
	public String preAddArticle(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
	/*	List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		
		
		//新增文章前查询出所有店铺
	//	List<Map<String, Object>> allShop = shopService.selectAllShop1();
	//	request.setAttribute("allShop", allShop);
	//	//新增文章钱查询出所有门票
	//	List<Map<String, Object>> allPromotionByFlagAndStatus = promotionService.findAllPromotionByFlagAndStatus();
	//	request.setAttribute("allTicket", allPromotionByFlagAndStatus);
		
		
		return "jsp/article/article-add";
		
	}
	
	
	/**
	 * 新增文章
	 * */
	@ResponseBody
	@RequestMapping("/addArticle.do")
	public int addArticle(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("a_status", 0);
		map.put("a_addtime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("文章插入  :  "+map);
		
		int re_status = articleService.addArticle(map);
		System.out.println("结果 : "+re_status);
		return re_status;
	}
	
	/**
	 * 查询所有文章
	 * */
	@RequestMapping("/selectAllArticle.do")
	public String selectAllArticle(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);
		
	
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allArticle = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = articleService.selectAllArticle(map).size();
		
		map.put("allArticleItem",itemCount);
		
		
		
		
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/article/article-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allArticle = articleService.selectAllArticle(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allArticle) {
			if(map2.get("a_addtime") != null) {
				int p = Integer.valueOf(map2.get("a_addtime").toString());
				if(p != 0) {
					map2.put("a_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		/*List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		request.setAttribute("allArticle",allArticle);
		return "jsp/article/article-list";
	}
	
	/**
	 * 查询所有文章,回收站
	 * */
	@RequestMapping("/selectAllArticleByDel.do")
	public String selectAllArticleByDel(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);
		
		
		map.put("a_status", 1);
		
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allArticle = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = articleService.selectAllArticle(map).size();
		
		map.put("allArticleItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/article/article-del-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allArticle = articleService.selectAllArticle(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allArticle) {
			if(map2.get("a_addtime") != null) {
				int p = Integer.valueOf(map2.get("a_addtime").toString());
				if(p != 0) {
					map2.put("a_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
	/*	List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		request.setAttribute("allArticle",allArticle);
		return "jsp/article/article-del-list";
	}
	
	
	/**
	 *修改前查询文章详情
	 * */
	@RequestMapping("/preSelectArticleDetails.do")
	public String preSelectArticleDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> articleDetails = articleService.selectArticleDetails(map);
		
		request.setAttribute("articleDetails",articleDetails);
		

		return "jsp/article/article-edit";
	}
	
	/**
	 * 修改文章
	 * */
	@ResponseBody
	@RequestMapping("/updateArticle.do")
	public int updateArticle(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("文章 : "+map);
		int shop = articleService.updateArticleDetails(map);
		return shop;
	}

	/**
	 * 查询所有文章板块
	 * */
	@RequestMapping("/selectAllArticleModu.do")
	public String selectAllArticleModu(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		List<Map<String, Object>> preAllModule = navigationService.selectAllModule();
		List<Map<String, Object>> allModule = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map2 : preAllModule) {
			if(map2.get("fm_img") !=null && !"".equals("fm_img")) {
				allModule.add(map2);
			}
		}
		request.setAttribute("allModule",allModule);
		return "jsp/article/article-modu-list";
	}

	
	/**
	 * 查询文章板块前查询
	 * */
	@RequestMapping("/selectArticleModuleDetails.do")
	public String selectArticleModuleDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
			
		Map<String, Object> moduleDetails = navigationService.selectArticleModuleDetails(map);
		request.setAttribute("moduleDetails", moduleDetails);
		return "jsp/article/article-modu-edit";
	}
	
	/**
	 * 修改板块
	 * */
	@ResponseBody
	@RequestMapping("/updateModuleDetails.do")
	public int updateModuleDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		int shop = navigationService.updateModuleDetails(map);
		return shop;
	}
	
	/**
	 * 修改文章
	 * */
	@ResponseBody
	@RequestMapping("/addV.do")
	public int addV(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("文章 : "+map);
		int shop = articleService.addV(map);
		return shop;
	}
}